package com.roger.uia;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.ContentResolver;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiObjectNotFoundException;

import com.roger.uia.helper.UiBase;

import org.junit.Before;
import org.junit.Test;

import static com.roger.uia.helper.UiBase.mDevice;
import static com.roger.uia.helper.UiBase.waitTime;

@SuppressLint("InvalidWakeLockTag")
public class TestPower {
    private static final String TAG = TestPower.class.getSimpleName();
    private PowerManager pm = (PowerManager)InstrumentationRegistry.getTargetContext().getApplicationContext().getSystemService(Context.POWER_SERVICE);
    private PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "myService");
    private ContentResolver cr = InstrumentationRegistry.getTargetContext().getContentResolver();
    @Before
    public void setUp()  {
        airPlaneMode_switch(true);
        mDevice.pressHome();
    }

    @Test
    public void Main(){
        try {
            wl.acquire();
                SystemClock.sleep(10000);
                mDevice.sleep();
                SystemClock.sleep(waitTime * 240);
                mDevice.wakeUp();
        } catch (Throwable e) {
            e.printStackTrace();
        }finally {
            wl.release();
        }
    }



    private void airPlaneMode_switch(Boolean on) {
        int isAirplaneMode = Settings.System.getInt(cr, Settings.System.AIRPLANE_MODE_ON, 0) ;
        if(on && isAirplaneMode==0 || !on && isAirplaneMode==1){
            mDevice.openQuickSettings();
            try {
                UiBase.waitClickIfObjExist(UiBase.objSelect[6],"飞行模式|Airplane mode",3);
            } catch (UiObjectNotFoundException e) {
                e.printStackTrace();
            }
        }
    }




    /*/
    -----------------------------------------------------other functions need key--------------------------------------------------------
     */

    @Test
    public void Wifi_on_off() {
        WifiManager wifiManager = (WifiManager) InstrumentationRegistry.getTargetContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        Bundle para= InstrumentationRegistry.getArguments();
        String key = para.getString("key");
        if(key.equals("on")){
             if (!wifiManager.isWifiEnabled()) {
                 wifiManager.setWifiEnabled(true);  ;
            }
         }else{
            if (wifiManager.isWifiEnabled()) {
                wifiManager.setWifiEnabled(false);
            }
         }
    }

    @Test
    public void BT_on_off() {
        BluetoothManager bluetoothManger = (BluetoothManager) InstrumentationRegistry.getTargetContext().getSystemService(Context.BLUETOOTH_SERVICE);
        BluetoothAdapter bluetoothadapter = bluetoothManger.getAdapter();
        Bundle para= InstrumentationRegistry.getArguments();
        String key = para.getString("key");
        if(key.equals("on")){
            if (!bluetoothadapter.isEnabled()) {
                bluetoothadapter.enable();
            }
        }else{
            if (bluetoothadapter.isEnabled()) {
                bluetoothadapter.disable();
            }
        }
    }

    @Test
    public void AirPlaneMode_on_off() {
//        无权限发送
//        Settings.System.putInt(cr,Settings.System.AIRPLANE_MODE_ON,0);
//        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
//        InstrumentationRegistry.getTargetContext().sendBroadcast(intent);
        Bundle para= InstrumentationRegistry.getArguments();
        String key = para.getString("key");
        int isAirplaneMode = Settings.System.getInt(cr, Settings.System.AIRPLANE_MODE_ON, 0) ;
        if(key.equals("on") && isAirplaneMode==0 || !key.equals("on") && isAirplaneMode==1){
            mDevice.openQuickSettings();
            try {
                UiBase.waitClickIfObjExist(UiBase.objSelect[6],"飞行模式|Airplane mode",3);
            } catch (UiObjectNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}
